package com.lonewalker.sparrow.domain;

import java.io.Serializable;

/**
 * @author lonewalker
 */
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 请求成功编码
     */
    public static final int SUCCESS_CODE = 200;
    /**
     * 请求成功提示信息
     */
    public static final String SUCCESS_MSG = "操作成功";
    /**
     * 请求失败编码
     */
    public static final int FAIL_CODE = 500;
    /**
     * 请求失败提示信息
     */
    public static final String FAIL_MSG = "服务器异常";
    /**
     * 标识
     */
    private int code;
    /**
     * 提示信息
     */
    private String msg;
    /**
     * 相关数据
     */
    private T data;



    public ApiResult() {
    }

    public ApiResult(int code, String msg){
        this.setCode(code);
        this.setMsg(msg);
    }

    public ApiResult(int code, String msg, T data) {
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }

    /**
     * 获取code
     * @return code
     */
    public Integer getCode() {
        return this.code;
    }

    /**
     * 获取msg
     * @return msg
     */
    public String getMsg() {
        return this.msg;
    }
    /**
     * 获取data
     * @return data
     */
    public T getData() {
        return this.data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }


    /**
     * 默认标识与提示信息
     */
    public static <T> ApiResult<T> success() {
        return new ApiResult<>(SUCCESS_CODE, SUCCESS_MSG, null);
    }

    /**
     * 成功：自定义提示信息
     */
    public static <T> ApiResult<T> successByMessage(String msg) {
        return new ApiResult<>(SUCCESS_CODE, SUCCESS_MSG, null);
    }

    /**
     * 成功：返回数据
     */
    public static <T> ApiResult<T> successByData(T data) {
        return new ApiResult<>(SUCCESS_CODE, SUCCESS_MSG, data);
    }

    /**
     * 失败：返回自定义错误码与提示信息
     */
    public static <T> ApiResult<T> error() {
        return new ApiResult<>(FAIL_CODE, FAIL_MSG, null);
    }

    /**
     * 失败：返回自定义提示信息
     */
    public static <T> ApiResult<T> errorByMessage(String msg) {
        return new ApiResult<>(FAIL_CODE, msg, null);
    }

    /**
     * 失败：返回自定义错误码与提示信息
     */
    public static <T> ApiResult<T> errorByCodeAndMessage(Integer code, String msg) {
        return new ApiResult<>(code, msg, null);
    }

    @Override
    public String toString() {
        return "{"
                + "\"code\": " + this.getCode()
                + ", \"msg\": \"" + this.getMsg() + "\""
                + ", \"data\": \"" + this.getData() + "\""
                + "}";
    }
}
